<template>
  <div class="app-container">

    <el-table 
      v-loading="listLoading" 
      :data="list" 
      border 
      fit 
      highlight-current-row 
      width="100%">
      <el-table-column 
        :label="$t('post.id')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('post.name')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('post.category')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ categoryName(scope.row.categoryId) }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('post.readNum')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.readNum }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('post.createtime')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('post.actions')" 
        align="center">
        <template slot-scope="scope">
          <router-link :to="'/post/edit/'+scope.row.id">
            <el-button 
              type="primary" 
              size="small" 
              icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button 
            size="small" 
            type="danger" 
            @click="del(scope.row.id)">{{ $t('all.del') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination 
      v-show="total>0" 
      :total="total" 
      :page.sync="listQuery.page" 
      :limit.sync="listQuery.pageSize" 
      @pagination="getList" />

  </div>
</template>

<script>
import { fetchList, remove,categoryList } from '@/api/article'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ArticleList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      categoryList:[],
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        order: 'desc',
        sort: 'id',
        filters: {
          name: '',
          categoryId: ''
        }
      }
    }
  },
  created() {
    categoryList().then(response => {
      this.categoryList = response.data
    })
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    del(row){
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        remove(row).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList();
        })
      }).catch(() => {

      })
    },
    categoryName($t){
      var name = '';
      this.categoryList.forEach((item,index)=>{
        if(item.id==$t){
          name = item.name;
        }
      });
      return name;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
